from django.contrib.auth.models import Group, Permission
from rest_framework.decorators import action
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from meiduo_admin.serialziers.group import GroupSerializer
from meiduo_admin.serialziers.permissions import PermissionSerializer
from meiduo_admin.utils import PageNum


class GroupView(ModelViewSet):
    #
    serializer_class = GroupSerializer
    queryset = Group.objects.all()
    # 分页
    pagination_class = PageNum
    permission_classes = [IsAdminUser]

    # methods指定路由请求方式，detail：是否需要正则匹配
    @action(methods=['get'],detail=False)
    def simple(self,request):
        '''获取权限数据'''
        data=Permission.objects.all()

        ser=PermissionSerializer(data,many=True)

        return Response(ser.data)
